package com.scitc.pleasurevideo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.scitc.pleasurevideo.R;

import java.util.Arrays;
import java.util.List;

public class DialogSeetingRadioAdapter extends ArrayAdapter {
    private Context context;
    private List<String> items;
    private Integer postionIndex;

    public DialogSeetingRadioAdapter(@NonNull Context context, List<String> items) {
        super(context, 0);
        this.items=items;
        this.context=context;
        this.postionIndex=0;
    }
    public DialogSeetingRadioAdapter(@NonNull Context context, String[] array) {
        super(context, 0);
        this.items= Arrays.asList(array);
        this.context=context;
        this.postionIndex=0;
    }

    @Override
    public int getCount() {
        if(items==null||items.size()<1){
            return 0;
        }
        return items.size();
    }

    @Override
    public int getItemViewType(int position) {
        if(position<0||position>=items.size()){
            return -1;
        }else if(position==postionIndex){
            return 0;
        }else {
            return 1;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    public void setPostionIndex(Integer postionIndex) {
        if(postionIndex<0||postionIndex>=items.size()){
            return;
        }
        this.postionIndex = postionIndex;
        notifyDataSetChanged();
    }

    private void clearSelect(){
        if(items==null||items.size()<1){
            return ;
        }
    }

    public Integer getPostionIndex() {
        if(postionIndex<0||postionIndex>=items.size()){
            return 0;
        }
        return postionIndex;
    }

    private View getCreateView(ViewGroup viewGroup){
        LayoutInflater layoutInflater=LayoutInflater.from(context);
        return layoutInflater.inflate(R.layout.item_single_text_center,viewGroup,false);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        String news=items.get(position);
        convertView=getCreateView(parent);
        TextView title=convertView.findViewById(R.id.title);
        title.setText(news);
        if(position==postionIndex){
            title.setTextColor(ContextCompat.getColor(context,R.color.main_color));
        }else {
            title.setTextColor(ContextCompat.getColor(context,R.color.radio_select_no));
        }
        return convertView;
    }
}
